#!/bin/sh
#
# Copyright (C) 2012 Paul Wouters <paul@libreswan.org>
#
# Based on Openswan version
#
# Called via mk/config.mk
#
# Options: IPSECVERSION is passed as ${1}, basedir as ${2}

usage() {
    echo "Usage: $0 <baseversion> [srctree]" >&2
    echo "       $0 --add-git-diry <baseversion> [srctree]" >&2
    exit 1
}

ADD_GIT_DIRTY=
if [ "${1}" = "--add-git-diry" ]; then ADD_GIT_DIRTY=' --dirty'; shift; fi

baseversion="${1}"
[ -n "${baseversion}" ] || usage

cd "${2:-.}" || usage

# -f .git check is needed for "git worktree"
if [ -d .git -o -f .git ]; then
    if git version >/dev/null 2>&1; then
	CUR=$(git describe --tags ${ADD_GIT_DIRTY})
	BRANCH=$(git rev-parse --abbrev-ref HEAD)
	echo "${CUR}-${BRANCH}"
	exit 0
    else
	echo "Error: git should be installed to determine exact Libreswan version" >&2
    fi
fi
echo "${baseversion}"
